﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using SAKS.Models;
using SAKS.Services;
using System.Drawing;
using SAKS.IServices;

namespace SAKS.UnitTest.ServicesTest
{
    [TestFixture]
    public class PictureServicesTest : TestBase
    {
        IPictureServices pictreService;
        public PictureServicesTest()
        {
            pictreService = new PictureServices();
        }
        [Test]
        public void PictureSaveAndGetTest()
        {
            var img = Image.FromFile("1.gif");
            Image thumbnail;
            Image original;
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            byte[] buff = ms.GetBuffer();
            Guid pictureId;

            using (var session = sessionFactory.OpenSession())
            {
                pictreService.Session = session;
                using (var tx = session.BeginTransaction())
                {
                    pictureId = pictreService.SavePictureFile(buff);
                    tx.Commit();
                }
            }

            using (var session = sessionFactory.OpenSession())
            {
                pictreService.Session = session;
                using (var tx = session.BeginTransaction())
                {
                    thumbnail = pictreService.GetThumbnail(pictureId);
                    tx.Commit();
                }
            }

            thumbnail.Save("thumb.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);

            using (var session = sessionFactory.OpenSession())
            {
                pictreService.Session = session;
                using (var tx = session.BeginTransaction())
                {
                    original = pictreService.GetOriginal(pictureId);
                    tx.Commit();
                }
            }

            original.Save("original.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
        }

    }
}
